-----------------------------------------------------------------------------------------
--
-- main.lua
--
-----------------------------------------------------------------------------------------

-- Your code here
local physics = require("physics")
--physics.setDrawMode( "hybrid" )
physics.start()

physics.setScale( 60 ) -- a value that seems good for small objects (based on playtesting)
physics.setGravity( 0, 0 ) -- overhead view, therefore no gravity vector
display.setStatusBar( display.HiddenStatusBar )

local tabletop = display.newImage( "table_bkg.png", true ) -- "true" overrides Corona's downsizing of large images on smaller devices
tabletop.x = 384
tabletop.y = 512

borderBodyElement = { friction=0.4, bounce=0.8}

local borderTop = display.newRect( 0, 0, display.viewableContentWidth, 1 )
borderTop:setFillColor( 0, 0, 0, 0)		-- make invisible
physics.addBody( borderTop, "static", borderBodyElement )

local borderBottom = display.newRect( 0, display.viewableContentHeight, display.viewableContentWidth, 1 )
borderBottom:setFillColor( 0, 0, 0, 0)		-- make invisible
physics.addBody( borderBottom, "static", borderBodyElement )

local borderLeft = display.newRect( 0, 1, 1, display.viewableContentHeight )
borderLeft:setFillColor( 0, 0, 0, 0)		-- make invisible
physics.addBody( borderLeft, "static", borderBodyElement )

local borderRight = display.newRect( display.viewableContentWidth-1, 1, 1, display.viewableContentHeight )
borderRight:setFillColor( 0, 0, 0, 0)		-- make invisible
physics.addBody( borderRight, "static", borderBodyElement )

local ball = {}
local ballColors = { "green", "yellow", "red", "blue" }
local ballBody = { density=0.8, friction=0.2, bounce=0.5, radius=15 }

-- Arrange balls in triangle formation
for i = 1, 4 do
ball[i] = display.newImage( "ball_" .. ballColors[i] .. ".png" )
ball[i].x = math.random(10, 768)
ball[i].y = math.random(20, 500)

physics.addBody( ball[i], ballBody )
ball[i].linearDamping = 0.3 -- simulates friction of felt
ball[i].angularDamping = 0.8 -- stops balls from spinning forever
ball[i].id = "ball" -- store object type as string attribute
ball[i].color = ballColors[n] -- store ball color as string attribute
end

-- Create cueball
local cueball = display.newImage( "ball_white.png" )
cueball.x = display.contentWidth/2; cueball.y = 780

physics.addBody( cueball, ballBody )
cueball.linearDamping = 0.3
cueball.angularDamping = 0.8
cueball.isBullet = true -- force continuous collision detection, to stop really fast shots from passing through other balls
cueball.color = "white"

target = display.newImage( "target.png" )
target.x = cueball.x; target.y = cueball.y; target.alpha = 0

local function resetCueball()
	cueball.alpha = 0
	cueball.x = 384
	cueball.y = 780
	cueball.xScale = 2.0; cueball.yScale = 2.0
	local dropBall = transition.to( cueball, { alpha=1.0, xScale=1.0, yScale=1.0, time=400 } )
end

-- Handler for ball in pocket
local gameOver -- forward declaration; function is below
local function inPocket( self, event )
	event.other:setLinearVelocity( 0, 0 )
	--local fallDown = transition.to( event.other, { alpha=0, xScale=0.3, yScale=0.3, time=200 } )
	
	if ( event.other.color == "white" ) then
		--timer.performWithDelay( 50, resetCueball )
	end
end

-- Add objects to use as collision sensors in the pockets
local sensorRadius = 5
-- Create pocket
local pocket = display.newCircle( 375, 250, sensorRadius )
-- (Change this value to "true" to make the pocket sensors visible)
pocket.isVisible = false		
physics.addBody( pocket, { radius=sensorRadius, isSensor=true } )
pocket.id = "pocket"
pocket.collision = inPocket
pocket:addEventListener( "collision", pocket ) -- add table listener to pocket sensor

-- Shoot the cue ball, using a visible force vector
local function cueShot( event )
	local t = event.target

	local phase = event.phase
	if "began" == phase then
		display.getCurrentStage():setFocus( t )
		t.isFocus = true
		
		-- Stop current cueball motion, if any
		t:setLinearVelocity( 0, 0 )
		t.angularVelocity = 0

		target.x = t.x
		target.y = t.y
		
		startRotation = function()
			target.rotation = target.rotation + 4
		end
		
		Runtime:addEventListener( "enterFrame", startRotation )
		
		local showTarget = transition.to( target, { alpha=0.4, xScale=0.4, yScale=0.4, time=200 } )
		myLine = nil

	elseif t.isFocus then
		if "moved" == phase then
			
			if ( myLine ) then
				myLine.parent:remove( myLine ) -- erase previous line, if any
			end
			myLine = display.newLine( t.x,t.y, event.x,event.y )
			myLine:setColor( 255, 255, 255, 50 )
			myLine.width = 8

		elseif "ended" == phase or "cancelled" == phase then
			display.getCurrentStage():setFocus( nil )
			t.isFocus = false
			
			local stopRotation = function()
				Runtime:removeEventListener( "enterFrame", startRotation )
			end
			
			local hideTarget = transition.to( target, { alpha=0, xScale=1.0, yScale=1.0, time=200, onComplete=stopRotation } )
			
			if ( myLine ) then
				myLine.parent:remove( myLine )
			end
			
			-- Strike the ball!
			t:applyForce( (t.x - event.x), (t.y - event.y), t.x, t.y )

		end
	end

	-- Stop further propagation of touch event
	return true
end

-- Activate the cue ball and start playing!
cueball:addEventListener( "touch", cueShot )
